package com.example.memosystem.controller;

import com.example.memosystem.model.Category;
import com.example.memosystem.model.Memo;
import com.example.memosystem.model.User;
import com.example.memosystem.service.CategoryService;
import com.example.memosystem.service.MemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/memo")
public class MemoController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private MemoService memoService;
    

    
    @GetMapping("/list")
    public String list(HttpSession session, Model model,
                      @RequestParam(required = false) String keyword,
                      @RequestParam(required = false) Integer categoryId) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        List<Memo> memos;
        if (keyword != null && !keyword.trim().isEmpty()) {
            memos = memoService.searchByTitle(user.getId(), keyword);
            model.addAttribute("keyword", keyword);
        } else if (categoryId != null) {
            memos = memoService.findByCategoryId(user.getId(), categoryId);
            Category category = categoryService.findById(categoryId, user.getId());
            model.addAttribute("category", category);
        } else {
            memos = memoService.findAllByUserId(user.getId());
        }
        
        List<Category> categories = categoryService.findAllByUserId(user.getId());
        
        model.addAttribute("memos", memos);
        model.addAttribute("categories", categories);
        return "memo/list";
    }
    
    @GetMapping("/add")
    public String addPage(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        List<Category> categories = categoryService.findAllByUserId(user.getId());
        model.addAttribute("categories", categories);
        
        return "memo/add";
    }
    
    @PostMapping("/add")
    public String add(Memo memo, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        memo.setUserId(user.getId());
        memoService.addMemo(memo);
        return "redirect:/memo/list";
    }
    
    @GetMapping("/edit/{id}")
    public String editPage(@PathVariable Integer id, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        Memo memo = memoService.findById(id, user.getId());
        if (memo == null) {
            return "redirect:/memo/list";
        }
        
        List<Category> categories = categoryService.findAllByUserId(user.getId());
        model.addAttribute("categories", categories);
        model.addAttribute("memo", memo);
        return "memo/edit";
    }

    @PostMapping("/edit")
    public String edit(Memo memo, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        memo.setUserId(user.getId());
        memoService.updateMemo(memo);
        return "redirect:/memo/list";
    }
    
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        memoService.deleteMemo(id, user.getId());
        return "redirect:/memo/list";
    }
    
    @GetMapping("/move-to-trash/{id}")
    public String moveToTrash(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        memoService.moveToTrash(id, user.getId());
        return "redirect:/memo/list";
    }
    
    @GetMapping("/restore/{id}")
    public String restore(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        memoService.restoreMemo(id, user.getId());
        return "redirect:/memo/recycle-bin";
    }
    
    @GetMapping("/delete-permanently/{id}")
    public String deletePermanently(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        memoService.deleteMemo(id, user.getId());
        return "redirect:/memo/recycle-bin";
    }
    
    @GetMapping("/empty-trash")
    public String emptyTrash(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        memoService.emptyTrash(user.getId());
        return "redirect:/memo/recycle-bin";
    }
    
    @GetMapping("/view/{id}")
    public String view(@PathVariable Integer id, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        Memo memo = memoService.findById(id, user.getId());
        if (memo == null) {
            return "redirect:/memo/list";
        }
        
        model.addAttribute("memo", memo);
        return "memo/view";
    }
    
    @GetMapping("/recycle-bin")
    public String recycleBin(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        
        List<Memo> trashedMemos = memoService.findTrashedMemos(user.getId());
        model.addAttribute("trashedMemos", trashedMemos);
        return "memo/recycle-bin";
    }
} 